package kr.co.doriclassic.chapter17;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
import java.awt.GridLayout;


public class RadioBtnEx extends JPanel implements ActionListener {
	private JLabel label;
	private JRadioButton tall;
	private JRadioButton grande;
	private JRadioButton venti;
	private JRadioButton trenta;
	private JPanel topPanel;
	private JPanel sizePanel;
	private JPanel resultPanel;
	private JLabel lvlResult;
	
	public RadioBtnEx() {
		initialize();
	}
	private void initialize() {
		setLayout(new BorderLayout(0, 0));
		
		topPanel = new JPanel();
		label = new JLabel("어떤 크기의 커피를 주문하시겠습니까");
		label.setHorizontalAlignment(SwingConstants.CENTER);
		topPanel.add(label);
		
		sizePanel = new JPanel();
		tall = new JRadioButton("Tall Size");
		tall.setVerticalAlignment(SwingConstants.TOP);
		tall.addActionListener(this);
		grande = new JRadioButton("Grande Size");
		grande.setVerticalAlignment(SwingConstants.TOP);
		grande.addActionListener(this);
		venti = new JRadioButton("Venti Size");
		venti.setVerticalAlignment(SwingConstants.TOP);
		venti.addActionListener(this);
		trenta = new JRadioButton("Trenta Size");
		trenta.setVerticalAlignment(SwingConstants.TOP);
		trenta.addActionListener(this);
		ButtonGroup size = new ButtonGroup();
		size.add(tall);
		size.add(grande);
		size.add(venti);
		size.add(trenta);
		sizePanel.setLayout(new GridLayout(0, 4, 0, 0));
		
		sizePanel.add(tall);
		sizePanel.add(grande);
		sizePanel.add(venti);
		sizePanel.add(trenta);
		
		resultPanel = new JPanel();
		lvlResult = new JLabel("크기를 선택하세요.");
		lvlResult.setForeground(Color.red);
		resultPanel.add(lvlResult);
		
		Border border = BorderFactory.createTitledBorder("크기");
		sizePanel.setBorder(border);
		add(topPanel, BorderLayout.NORTH);
		add(sizePanel,BorderLayout.CENTER);
		add(resultPanel,BorderLayout.SOUTH);
		
	}

	public void actionPerformed(ActionEvent e) {
		String[] strs = {"tall", "grande", "venti", "trenta"};
		ImageIcon[] icons = new ImageIcon[4];
		for (int i = 0; i < icons.length; i++) {
			icons[i] = new ImageIcon(strs[i]+".jpg");
		}
		
		lvlResult.setForeground(Color.blue);
		if(e.getSource()==tall){
			lvlResult.setIcon(icons[0]);
			lvlResult.setText("");
		}else if(e.getSource()==grande){
			lvlResult.setIcon(icons[1]);
			lvlResult.setText("");
		}else if(e.getSource()==venti){
			lvlResult.setIcon(icons[2]);
			lvlResult.setText("");
		}else if(e.getSource()==trenta){
		lvlResult.setIcon(icons[3]);
		lvlResult.setText("");
		}
	}	
}
